package edu.hm.webengineer.meetagain.server;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import edu.hm.webengineer.meetagain.client.GreetingService;
import edu.hm.webengineer.meetagain.shared.FieldVerifier;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

/**
 * The server side implementation of the RPC service.
 */
@SuppressWarnings("serial")
public class GreetingServiceImpl extends RemoteServiceServlet implements
		GreetingService {

	public String greetServer(String input) throws IllegalArgumentException {
		// Verify that the input is valid. 
		if (!FieldVerifier.isValidName(input)) {
			// If the input is not valid, throw an IllegalArgumentException back to
			// the client.
			throw new IllegalArgumentException(
					"Name must be at least 4 characters long");
		}

		String serverInfo = getServletContext().getServerInfo();
		String userAgent = getThreadLocalRequest().getHeader("User-Agent");

		// Escape data from the client to avoid cross-site script vulnerabilities.
		input = escapeHtml(input);
		userAgent = escapeHtml(userAgent);

		return "Hello, " + input + "!<br><br>I am running " + serverInfo
				+ ".<br><br>It looks like you are using:<br>" + userAgent;
	}

	/**
	 * Escape an html string. Escaping data received from the client helps to
	 * prevent cross-site script vulnerabilities.
	 * 
	 * @param html the html string to escape
	 * @return the escaped string
	 */
	private String escapeHtml(String html) {
		if (html == null) {
			return null;
		}
		return html.replaceAll("&", "&amp;").replaceAll("<", "&lt;")
				.replaceAll(">", "&gt;");
	}
	
	// mysql connectivity method
	private static Connection SQLConnect(String iniPath)
	{
				//mysql connectivity params
				Connection conn=null;
				
				/*
				
				   BufferedReader b = new BufferedReader(new InputStreamReader(new FileInputStream(new File(iniPath))));
				
				   here we will read in sql connection params from a sql.ini file in our package later on
				   and put the params of each line in the Strings with b.readLine()
				*/
				
				
				String dbHost = "localhost";
				String dbPort = "3306";
				String dbScheme = "test";
				String dbUser = "meet";
				String dbPwd =  "again";
				
				
				try
				{
					// Load the MySQL JDBC driver
					Class.forName("com.mysql.jdbc.Driver") ;
					System.out.println("MySQL JDBC driver loaded ok.");	 
				} 
				catch (ClassNotFoundException e)
				{
						System.err.println("Exception: "+e.getMessage());
				}
				//driver manager to sql database scheme meetagain
				try 
				{ 
					   conn = DriverManager.getConnection("jdbc:mysql://" + dbHost + ":" + dbPort + "/" + dbScheme + "?user="+dbUser+"&password="+dbPwd);
					   System.out.println("SQL Connection successfully established");
					   return conn;
				}
				catch (SQLException ex)
				{
					System.err.println("Exception: "+ex.getMessage());
				}
				return conn;
		}		

}
